﻿using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WepApi07.Data.Entity;
using WepApi07.Domain;
using WepApi07.Domain.Enum;
using WepApi07.Intenface.Log;


namespace WepApi07.Implement.Log
{
    public class Log : ILog
    {
        private readonly IResponsitory<Logs> _logsResponsitory;

        

        public Log(IResponsitory<Logs> logsResponsitory)
        {
            _logsResponsitory = logsResponsitory;
        }
        
        public void Clear()
        {
            var list = _logsResponsitory.Table.ToList();
            _logsResponsitory.Delete(list);
        }

        public void Delete(Logs log)
        {
            _logsResponsitory.Delete(log);
        }

        public Logs GetLogById(int id)
        {
            return _logsResponsitory.GetById(id);
        }

        public void Insert(Logs log)
        {
            _logsResponsitory.Insert(log);
        }

        public IEnumerable<Logs> MultiQuery(DateTime beginTime, DateTime endTime, LogsLevel logLevel=LogsLevel.错误, int userId = 0)
        {
            var bTime = beginTime == null ? DateTime.Now : beginTime;
            var eTime = endTime == null ? DateTime.Now : endTime;

            var list = _logsResponsitory.Table.Where(x => x.CreatedTime >= bTime && x.CreatedTime <= eTime && x.LogLevel==(int)logLevel);

            if (userId != 0)
            {
                list = list.Where(x => x.UserId == userId);
            }

            return list;
        }
    }
}
